/*
 *      kernel_notebook.cpp
 *      
 *      Copyright 2009 AzureSky <AzureSky.fedora@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#include "kernel_notebook.h"

kernel_notebook::kernel_notebook(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade):
	Gtk::Notebook(cobject),
	refXml(refGlade)
{
	kernel_page();
	vm_page();
	filesys_page();
	net_page();
}

kernel_notebook::~kernel_notebook()
{
}

void kernel_notebook::kernel_page()
{
	
	int result;
	/*ctl_alt_del*/
	refXml -> get_widget("check_ctl_alt_del",ctl_alt_del);
	result = kernel.about_ctrl_alt_del();
	result != -1 ? ctl_alt_del -> set_active(result) : ctl_alt_del -> hide();
	ctl_alt_del -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_ctrl_alt_del) );
	
	/*max threads*/
	refXml -> get_widget("spin_threads_max",threads);
	result = kernel.about_threads_max();
	result != -1 ? threads -> set_value(result) : threads -> hide();
	threads -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_threads_max) );
	
	/*panic*/
	refXml -> get_widget("spin_panic",panic);
	result = kernel.about_panic();
	result != -1 ? panic -> set_value(result) : panic -> hide();
	panic -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_panic) );
	
	/*msgmax*/
	refXml -> get_widget("spin_msgmax",msgmax);
	result = kernel.about_msgmax();
	result != -1 ? msgmax -> set_value(result) : msgmax -> hide();
	msgmax -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_msgmax) );
	
	/*msgmnb*/
	refXml -> get_widget("spin_msgmnb",msgmnb);
	result = kernel.about_msgmnb();
	result != -1 ? msgmnb -> set_value(result) : msgmnb -> hide();
	msgmnb -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_msgmnb) );
	
	/*msgmni*/
	refXml -> get_widget("spin_msgmni",msgmni);
	result = kernel.about_msgmni();
	result != -1 ? msgmni -> set_value(result) : msgmni -> hide();
	msgmni -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_msgmni) );
	
	/*shmall*/
	refXml -> get_widget("spin_shmall",shmall);
	result = kernel.about_shmall();
	result != -1 ? shmall -> set_value(result) : shmall -> hide();
	shmall -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_shmall) );
	
	/*shmax*/
	refXml -> get_widget("spin_shmmax",shmmax);
	long value = kernel.about_shmmax();
	value != -1 ? shmmax -> set_value(value) : shmmax -> hide();
	shmmax -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_shmmax) );
	
	/*shmmni*/
	refXml -> get_widget("spin_shmmni",shmmni);
	result = kernel.about_shmmni();
	result != -1 ? shmmni -> set_value(result) : shmmni -> hide();
	shmmni -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_shmmni) );
	
}

void kernel_notebook::change_ctrl_alt_del()
{

	ctl_alt_del -> set_active( kernel.change_ctrl_alt_del(ctl_alt_del -> get_active()) );

}

void kernel_notebook::change_threads_max()
{

	threads -> set_value( kernel.change_threads_max(threads -> get_value_as_int()) );

}

void kernel_notebook::change_panic()
{

	panic -> set_value( kernel.change_panic(panic -> get_value_as_int()) );

}

void kernel_notebook::change_msgmax()
{

	msgmax -> set_value( kernel.change_msgmax(msgmax -> get_value_as_int()) );

}

void kernel_notebook::change_msgmnb()
{

	msgmnb -> set_value( kernel.change_msgmnb(msgmnb -> get_value_as_int()) );

}

void kernel_notebook::change_msgmni()
{

	msgmni -> set_value( kernel.change_msgmni(msgmni -> get_value_as_int()) );

}

void kernel_notebook::change_shmall()
{

	shmall -> set_value( kernel.change_shmall(shmall -> get_value_as_int()) );

}

void kernel_notebook::change_shmmax()
{

	shmmax -> set_value( kernel.change_shmmax(shmmax -> get_value()) );

}

void kernel_notebook::change_shmmni()
{

	shmmni -> set_value( kernel.change_shmmni(shmmni -> get_value_as_int()) );

}

/*notebook: vm*/
void kernel_notebook::vm_page()
{
	
	int result;
	/*swappiness*/
	refXml -> get_widget("hscale_swap",swap_scale);
	swap_scale -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_swap) );
	
	result = vm.about_swappiness();
	swap_scale -> set_value(result);
	

}

void kernel_notebook::change_swap()
{

	int result = swap_scale -> get_value();
	
	result = vm.change_swappiness(result);

}

/*notebook: filesystem */
void kernel_notebook::filesys_page()
{
	
	int result;
	/*max file*/
	refXml -> get_widget("spin_max_file",max_file);
	result = filesys.about_max_file();
	result != -1 ? max_file -> set_value(result) : max_file -> hide();
	max_file -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_max_file) );
	

}

void kernel_notebook::change_max_file()
{

	max_file -> set_value( filesys.change_max_file(max_file -> get_value_as_int()) );

}

/*notebook: network */
void kernel_notebook::net_page()
{
	
	int result = 0;
	
	/*sack*/
	refXml -> get_widget("check_sack",sack);
	result = net_work.about_ipv4_tcp_sack();
	result != -1 ? sack -> set_active(result) : sack -> hide();
	sack -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_sack) );
	
	/*fack*/
	refXml -> get_widget("check_fack",fack);
	result = net_work.about_ipv4_tcp_fack();
	result != -1 ? fack -> set_active(result) : fack -> hide();
	fack -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_fack) );
	
	/*timestamps*/
	refXml -> get_widget("check_time_out",time_out);
	result = net_work.about_ipv4_tcp_timestamps();
	result != -1 ? time_out -> set_active(result) : time_out -> hide();
	time_out -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_time_out) );
	
	/*ip forward*/
	refXml -> get_widget("check_forward",ip_forward);
	result = net_work.about_ipv4_ip_forward();
	result != -1 ? ip_forward -> set_active(result) : ip_forward -> hide();
	ip_forward -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_forward) );
	
	/*win scaling*/
	refXml -> get_widget("check_win_scal",win_scal);
	result = net_work.about_ipv4_tcp_win_scaling();
	result != -1 ? win_scal -> set_active(result) : win_scal -> hide();
	win_scal -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_win_scal) );
	
	/*low latency*/
	refXml -> get_widget("chec_low_latency",low_latency);
	result = net_work.about_ipv4_tcp_low_latency();
	result != -1 ? low_latency -> set_active(result) : low_latency -> hide();
	low_latency -> signal_clicked().connect(sigc::mem_fun(*this,&kernel_notebook::change_low_latency) );
	
	
	long value = 0;
	
	/*rmem default*/
	refXml -> get_widget("spin_rmen_default",rmem_default);
	value = net_work.about_core_rmem_default();
	value != -1 ? rmem_default -> set_value(value) : rmem_default -> hide();
	rmem_default -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_rmem_default) );
	
	/*rmem max*/
	refXml -> get_widget("spin_rmem_max",rmem_max);
	value = net_work.about_core_rmem_max();
	value != -1 ? rmem_max -> set_value(value) : rmem_max -> hide();
	rmem_max -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_rmem_max) );
	
	/*wmem default*/
	refXml -> get_widget("spin_wmem_default",wmem_default);
	value = net_work.about_core_wmem_default();
	value != -1 ? wmem_default -> set_value(value) : wmem_default -> hide();
	wmem_default -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_wmem_default) );
	
	/*wmem max*/
	refXml -> get_widget("spin_wmem_max",wmem_max);
	value = net_work.about_core_wmem_max();
	value != -1 ? wmem_max -> set_value(value) : wmem_max -> hide();
	wmem_max -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_wmem_max) );
	
	
	tcp_mem three_value;
	
	/*tcp mem size*/
	refXml -> get_widget("tcp_mem_min",tcp_mem_min);
	refXml -> get_widget("tcp_mem_default",tcp_mem_default);
	refXml -> get_widget("tcp_mem_max",tcp_mem_max);
	three_value = net_work.about_tcp_mem();
	if(three_value.min_mem != -1){
		tcp_mem_min -> set_value(three_value.min_mem);
		tcp_mem_default -> set_value(three_value.default_mem);
		tcp_mem_max -> set_value(three_value.max_mem);
	}
	tcp_mem_min -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_mem) );
	tcp_mem_default -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_mem) );
	tcp_mem_max -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_mem) );
	
	/*tcp wmem size*/
	refXml -> get_widget("tcp_wmem_min",tcp_wmem_min);
	refXml -> get_widget("tcp_wmem_default",tcp_wmem_default);
	refXml -> get_widget("tcp_wmem_max",tcp_wmem_max);
	three_value = net_work.about_tcp_wmem();
	if(three_value.min_mem != -1){
		tcp_wmem_min -> set_value(three_value.min_mem);
		tcp_wmem_default -> set_value(three_value.default_mem);
		tcp_wmem_max -> set_value(three_value.max_mem);
	}
	tcp_wmem_min -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_wmem) );
	tcp_wmem_default -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_wmem) );
	tcp_wmem_max -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_wmem) );
	
	/*tcp rmem size*/
	refXml -> get_widget("tcp_rmem_min",tcp_rmem_min);
	refXml -> get_widget("tcp_rmem_default",tcp_rmem_default);
	refXml -> get_widget("tcp_rmem_max",tcp_rmem_max);
	three_value = net_work.about_tcp_rmem();
	if(three_value.min_mem != -1){
		tcp_rmem_min -> set_value(three_value.min_mem);
		tcp_rmem_default -> set_value(three_value.default_mem);
		tcp_rmem_max -> set_value(three_value.max_mem);
	}
	tcp_rmem_min -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_rmem) );
	tcp_rmem_default -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_rmem) );
	tcp_rmem_max -> signal_value_changed().connect(sigc::mem_fun(*this,&kernel_notebook::change_tcp_rmem) );
	

}

void kernel_notebook::change_sack()
{

	sack -> set_active( net_work.change_ipv4_tcp_sack(sack -> get_active()) );

}

void kernel_notebook::change_fack()
{

	fack -> set_active( net_work.change_ipv4_tcp_fack(fack -> get_active()) );

}

void kernel_notebook::change_forward()
{

	ip_forward -> set_active( net_work.change_ipv4_ip_forward(ip_forward -> get_active()) );

}

void kernel_notebook::change_win_scal()
{

	win_scal -> set_active( net_work.change_ipv4_tcp_win_scaling(win_scal -> get_active()) );

}

void kernel_notebook::change_time_out()
{

	time_out -> set_active( net_work.change_ipv4_tcp_timestamps(time_out -> get_active()) );

}

void kernel_notebook::change_low_latency()
{

	low_latency -> set_active( net_work.change_ipv4_tcp_low_latency(low_latency -> get_active()) );

}
		
void kernel_notebook::change_rmem_default()
{

	rmem_default -> set_value( net_work.change_core_wmem_max(rmem_default -> get_value()) );

}

void kernel_notebook::change_rmem_max()
{

	rmem_max -> set_value( net_work.change_core_wmem_max(rmem_max -> get_value()) );

}

void kernel_notebook::change_wmem_default()
{

	wmem_default -> set_value( net_work.change_core_wmem_max(wmem_default -> get_value()) );
;
}

void kernel_notebook::change_wmem_max()
{

	wmem_max -> set_value( net_work.change_core_wmem_max(wmem_max -> get_value()) );

}

void kernel_notebook::change_tcp_mem()
{

	tcp_mem mem_temp;
	mem_temp.min_mem = tcp_mem_min -> get_value();
	mem_temp.default_mem = tcp_mem_default -> get_value();
	mem_temp.max_mem = tcp_mem_max -> get_value();
	
	mem_temp = net_work.change_tcp_mem(mem_temp);

}

void kernel_notebook::change_tcp_rmem()
{

	tcp_mem rmem_temp;
	rmem_temp.min_mem = tcp_rmem_min -> get_value();
	rmem_temp.default_mem = tcp_rmem_default -> get_value();
	rmem_temp.max_mem = tcp_rmem_max -> get_value();
	
	rmem_temp = net_work.change_tcp_rmem(rmem_temp);

}

void kernel_notebook::change_tcp_wmem()
{

	tcp_mem wmem_temp;
	wmem_temp.min_mem = tcp_wmem_min -> get_value();
	wmem_temp.default_mem = tcp_wmem_default -> get_value();
	wmem_temp.max_mem = tcp_wmem_max -> get_value();
	
	wmem_temp = net_work.change_tcp_wmem(wmem_temp);

}
